Dynomotion

Group: DynoMotion Message: 1285 From: morgtod Date: 6/16/2011
Subject: c example
I am trying to use a couple of the example c files in a single code. How would you program a for (::) like in the external feedhold example, along with another for(::) with a delay_sec(1.0), like in the show steprate example?

I have 2 oil level switches, 2 air flow sensors and 3 proximity sensors, 1 spindle thermal switch, 1 spindle thermistor and a spindle chiller flow sensor that I need to fit into for(;;) loops, but some need delays and the delays cannot effect the other loops.

Thanks!
Group: DynoMotion Message: 1289 From: albertplatek Date: 6/16/2011
Subject: Re: c example
Hi Todd,

I dont know if it is a good solution, but it should work:

#include "KMotionDef.h"
#define Delay1 0.5
#define Delay2 0.1
main()
{
SetBitDirection(46,1); //D1 LED on KFLOP
SetBitDirection(47,1); //D2 LED on KFLOP

double T1=Time_sec()+Delay1;
double T2=Time_sec()+Delay2;
int d1led=0;
int d2led=0;

for(;;)
{
WaitNextTimeSlice();

if(T1<Time_sec())
{
d1led=~d1led;
SetStateBit(46,d1led);
T1=Time_sec()+Delay1;
}

if(T2<Time_sec())
{
d2led=~d2led;
SetStateBit(47,d2led);
T2=Time_sec()+Delay2;
}

}
}

Best Regards
Albert P³atek

--- In DynoMotion@yahoogroups.com, "morgtod" <todmorg@...> wrote:
>
> I am trying to use a couple of the example c files in a single code. How would you program a for (::) like in the external feedhold example, along with another for(::) with a delay_sec(1.0), like in the show steprate example?
>
> I have 2 oil level switches, 2 air flow sensors and 3 proximity sensors, 1 spindle thermal switch, 1 spindle thermistor and a spindle chiller flow sensor that I need to fit into for(;;) loops, but some need delays and the delays cannot effect the other loops.
>
> Thanks!
>
Group: DynoMotion Message: 1290 From: albertplatek Date: 6/16/2011
Subject: Re: c example
Hi Thodd,

Another example but for input pin delay

//Example how to use input pin with timer delay

#include "KMotionDef.h"
#define ButtonDelay 1
main()
{
SetBitDirection(46,1);
double T1;

for(;;)
{
WaitNextTimeSlice();

if(!ReadBit(0))
{
SetStateBit(46,0);
T1=Time_sec()+(ButtonDelay);
}
if((T1<Time_sec())&ReadBit(0))
{
SetStateBit(46,1);
}
}
}

Regards
Albert P³atek

--- In DynoMotion@yahoogroups.com, "albertplatek" <albertplatek86@...> wrote:
>
> Hi Todd,
>
> I dont know if it is a good solution, but it should work:
>
> #include "KMotionDef.h"
> #define Delay1 0.5
> #define Delay2 0.1
> main()
> {
> SetBitDirection(46,1); //D1 LED on KFLOP
> SetBitDirection(47,1); //D2 LED on KFLOP
>
> double T1=Time_sec()+Delay1;
> double T2=Time_sec()+Delay2;
> int d1led=0;
> int d2led=0;
>
> for(;;)
> {
> WaitNextTimeSlice();
>
> if(T1<Time_sec())
> {
> d1led=~d1led;
> SetStateBit(46,d1led);
> T1=Time_sec()+Delay1;
> }
>
> if(T2<Time_sec())
> {
> d2led=~d2led;
> SetStateBit(47,d2led);
> T2=Time_sec()+Delay2;
> }
>
> }
> }
>
> Best Regards
> Albert P³atek
>
> --- In DynoMotion@yahoogroups.com, "morgtod" <todmorg@> wrote:
> >
> > I am trying to use a couple of the example c files in a single code. How would you program a for (::) like in the external feedhold example, along with another for(::) with a delay_sec(1.0), like in the show steprate example?
> >
> > I have 2 oil level switches, 2 air flow sensors and 3 proximity sensors, 1 spindle thermal switch, 1 spindle thermistor and a spindle chiller flow sensor that I need to fit into for(;;) loops, but some need delays and the delays cannot effect the other loops.
> >
> > Thanks!
> >
>
Group: DynoMotion Message: 1291 From: Tom Kerekes Date: 6/16/2011
Subject: Re: c example
Hi Todd,
 
One common and classic programming technique is called a "State Machine" that can be used to control multiple concurrent sequences with only one CPU thread.  The fundamental idea is that each independent task must remember its current "state" so that the code can exit, go off and do other things, then come back and return to what it was doing.
 
The first step in using the method is to define "states" for each task.  A state is defined for each mode that might persist for a "long" time.  This is normally when waiting for a timer or something external to happen.  Things that can be performed instantaneously do not need separate states.  If a state decides that it is time to switch to a different state, it just changes the state variable to the new state.
 
I like to use the structure shown below because it separates all the code and data for each task and is fairly readable and is expandable to any number of tasks and any number of states in each task.
 
If you aren't familiar with a C Language "switch" statement it can be used instead of number of if (State==XXX) statements.
 
Regards
TK
 
 
#include "KMotionDef.h"
 
void ServicePrintRate();
void ServiceTask2();
void ServiceTask3();
void ServiceTask4();
 
main()
{
    for (;;)
    {
        WaitNextTimeSlice();
        ServicePrintRate();
        ServiceTask2();
        ServiceTask3();
        ServiceTask4();
    }
}
 
#define RATE_INIT 0
#define RATE_DELAY 1
 
int RateState=RATE_INIT;
double t0,t1,new,old;
 
void ServicePrintRate()
{
    switch (RateState)
    {
        case RATE_INIT:  // setup to begin a measurement
           old = ch4->Dest;
           t0=Time_sec();
           RateState=RATE_DELAY;  // goto delay state
           break;
  
        case RATE_DELAY:
           if (Time_sec() > t0 + 1.0) // delay finished?
           {
               new = ch4->Dest;  // yes, finish measurement
               t1=Time_sec();
               printf("Rate = %13.2f steps/sec\n",(new-old)/(t1-t0));
               RateState=RATE_INIT;  // return to initial state
           }
           break;
    }
}
 
void ServiceTask2()
{
}
 
void ServiceTask3()
{
}
 
void ServiceTask4()
{
}
 
 


--- On Thu, 6/16/11, morgtod <todmorg@...> wrote:

From: morgtod <todmorg@...>
Subject: [DynoMotion] c example
To: DynoMotion@yahoogroups.com
Date: Thursday, June 16, 2011, 6:38 AM

 
I am trying to use a couple of the example c files in a single code. How would you program a for (::) like in the external feedhold example, along with another for(::) with a delay_sec(1.0), like in the show steprate example?

I have 2 oil level switches, 2 air flow sensors and 3 proximity sensors, 1 spindle thermal switch, 1 spindle thermistor and a spindle chiller flow sensor that I need to fit into for(;;) loops, but some need delays and the delays cannot effect the other loops.

Thanks!